1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.webmacro;
24
25 import org.webmacro.ResourceException;
26 import org.webmacro.Template;
27 import org.webmacro.resource.AbstractTemplateLoader;
28 import org.webmacro.resource.CacheElement;
29
30 import java.io.File;
31 import java.net.URL;
32 import java.net.MalformedURLException;
33
34 /***
35 * Implementation of TemplateLoader that loads templates from a given URL.
36 * Objects of this class are responsible for searching exactly one
37 * directory for templates. If it handles a request, it takes URL as
38 * the base path to find the template.
39 * @author Marc Palmer (marc@anyware.co.uk)
40 */
41 public class URLTemplateLoader extends AbstractTemplateLoader
42 {
43
44 private URL baseURI;
45
46 public void setConfig (String config)
47 {
48 try
49 {
50 this.baseURI = new URL(config);
51 }
52 catch (MalformedURLException e)
53 {
54 log.error("Cannot init url template loader, bad URL", e);
55 }
56 }
57
58 /***
59 * Tries to load a template by interpreting query as
60 * a path relative to the path set by setPath.
61 */
62 public final Template load (String query, CacheElement ce) throws ResourceException
63 {
64 try
65 {
66 URL url = new URL(baseURI, query);
67 if (log.loggingDebug())
68 {
69 log.debug("FileTemplateProvider: Found template " + url);
70 }
71 return helper.load(url, ce);
72 }
73 catch (MalformedURLException e)
74 {
75 throw new ResourceException("Bad template URL", e);
76 }
77 }
78 }